package table;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import table.tools.NameValues;


/**
 * @author sq
 *
 */
public class Row extends NameValues{
	public static List<Row> getRowsFromCols(List<Col> cols){
		List<Row> results = new ArrayList<Row>();
		try{
			int rowSize = cols.get(0).getDataSize();
			for(int rowIndex = 0; rowIndex < rowSize; rowIndex++){
				Row nRow = new Row();
				for(int colIndex = 0; colIndex < cols.size(); colIndex++){
					nRow.setValueByName(cols.get(colIndex).getName()
							, cols.get(colIndex).getValue(rowIndex));
				}
				results.add(nRow);
			}
		}catch(Exception e){
			
		}
		
		return results;
	}
	public static List<Row> getRowsFromResquest(HttpServletRequest request){
		List<Row> results = new ArrayList<Row>();
		//$RD_$i_name=value
		for(int i=0; i < 65535; i++){
			String prefix = "$RD_$" + i + "_";
			Row iRow = new Row();
			for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements() ;) {
				String tName = e.nextElement();
				if(tName.startsWith(prefix)){
					String tValue = request.getParameter(tName);
					iRow.setValueByName(tName.substring(prefix.length()), tValue);
				}
			}
			if(iRow.getNameList() != null && iRow.getNameList().size() > 0){
				results.add(iRow);
			} else {
				break;
			}
		}
		return results;
	}
	
	public Row(){
	}
	
	public Row(HttpServletRequest request) {
	  for (Enumeration<String> e = request.getParameterNames(); e.hasMoreElements() ;) {
	     String tName = e.nextElement();
	     Object tValue = request.getParameter(tName);
	     this.setValueByName(tName, tValue);
	  }
	}

}
